package org.maxgroundstation.simulation.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JPanel;

import org.maxgroundstation.simulation.MaxRobot;


/**
 * A panel for displaying the MAX robot from the top down.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
@SuppressWarnings("serial")
public class TopDownPanel extends JPanel {

    // Data members.
    private MaxRobot robot;
    
    /**
     * Constructor
     * @param robot the simulated MAX robot.
     */
    public TopDownPanel(MaxRobot robot) {
        // Use JPanel constructor.
        super();
        
        this.robot = robot;
        
        setPreferredSize(new Dimension(200, 400));
        
        setBackground(Color.BLACK);
        setOpaque(true);
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        // Draw chasis.
        drawChasis(g2d);
        
        // Draw wheels.
        drawWheels(g2d);
        
        // Draw turret.
        drawTurret(g2d);
    }
    
    /**
     * Draws the robot chasis.
     * @param g2d the graphics context.
     */
    private void drawChasis(Graphics2D g2d) {
        
        g2d.setColor(Color.WHITE);
        
        g2d.draw(new Rectangle2D.Float(50, 100, 100, 200));
    }
    
    /**
     * Draws the four wheels.
     * @param g2d the graphics context.
     */
    private void drawWheels(Graphics2D g2d) {
        
        g2d.setColor(Color.WHITE);
        
        double frontAngle = robot.getFrontWheelsTurnAngle();
        double rearAngle = robot.getRearWheelsTurnAngle();
        
        // Front left wheel.
        g2d.rotate(frontAngle, 50D, 100D);
        g2d.draw(new RoundRectangle2D.Float(25F, 62.5F, 50F, 75F, 25F, 25F));
        g2d.rotate(0D - frontAngle, 50D, 100D);
        
        // Front right wheel.
        g2d.rotate(frontAngle, 150D, 100D);
        g2d.draw(new RoundRectangle2D.Float(125F, 62.5F, 50F, 75F, 25F, 25F));
        g2d.rotate(0D - frontAngle, 150D, 100D);
        
        // Rear left wheel.
        g2d.rotate(rearAngle, 50D, 300D);
        g2d.draw(new RoundRectangle2D.Float(25F, 262.5F, 50F, 75F, 25F, 25F));
        g2d.rotate(0D - rearAngle, 50D, 300D);
        
        // Rear right wheel.
        g2d.rotate(rearAngle, 150D, 300D);
        g2d.draw(new RoundRectangle2D.Float(125F, 262.5F, 50F, 75F, 25F, 25F));
        g2d.rotate(0D - rearAngle, 150D, 300D);
    }
    
    /**
     * Draws the head turret.
     * @param g2d the graphics context.
     */
    private void drawTurret(Graphics2D g2d) {
        
        g2d.setColor(Color.WHITE);
        
        double panAngle = robot.getHeadPanAngle();
        
        g2d.rotate(panAngle, 100D, 200D);
        
        g2d.draw(new Ellipse2D.Float(50F, 150F, 100F, 100F));
        
        g2d.draw(new Rectangle2D.Float(50F, 195F, 100F, 10F));
        
        g2d.draw(new Rectangle2D.Float(70F, 185F, 20F, 10F));
        
        g2d.draw(new Rectangle2D.Float(110F, 185F, 20F, 10F));
        
        g2d.rotate(0D - panAngle, 100D, 200D);
    }
}